/*
 * Copyright [2018] [Alex/libo(liboware@gmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyt.tomcat.handler;

import com.hyt.tomcat.bean.TomcatBean;
import com.hyt.tomcat.config.GlobalConfiguration;
import com.hyt.tomcat.config.TomcatBootType;
import com.hyt.tomcat.config.TomcatProperties;
import com.hyts.guava.String.Strings;
import com.hyts.properties.manager.PropertyManager;

import java.lang.reflect.Field;

/**
 * @author LiBo/Alex
 * @see com.hyt.tomcat.config.LoadConfigLevel 加载配置文件的级别-数字小的优先级越高
 * @version V1.0
 */
public class LoadConfigHandler {

    /**
     * <b>loadConfig</b>
     * @param configLevel configLevel-配置级别
     * @exception
     */
    public static TomcatBean loadConfig(TomcatBootType configLevel) {
        TomcatBean tomcatBean = new TomcatBean();
        TomcatProperties tomcatProperties = new TomcatProperties();
        loadConfigFromFile(tomcatProperties);
        if(Strings.isBlank(tomcatProperties.getWebappDirPath())){
            tomcatProperties.setWebappDirPath(configLevel.getValue());
        }
        tomcatBean.setTomcatProperties(tomcatProperties);
        tomcatBean.setBootType(configLevel);
        return tomcatBean;
    }

    /**
     * loadConfigFromEnv
     * 加载配置信息从环境变量
     * @return
     */
    private static boolean loadConfigFromEnv(TomcatProperties tomcatProperties) {
        return false;
    }

    /**
     * 加载配置信息从配置文件
     * @param tomcatProperties
     * @return
     */
    private static TomcatProperties loadConfigFromFile(TomcatProperties tomcatProperties) {
        PropertyManager propertyManager = PropertyManager.getInstance().settingDefaultConfigurationName(GlobalConfiguration.DEFAULT_LOAD_TOMCAT_PROPERTIES_PATH);
        try {
         for(String param:GlobalConfiguration.PROPERTIES_CONFIGURATION){
            String value = propertyManager.getValue2Text(GlobalConfiguration.DEFAULT_LOAD_CONFIG_TOMCAT_COMMAND+param);
                Field field = tomcatProperties.getClass().getDeclaredField(param);
                field.setAccessible(true);
                field.set(tomcatProperties,value);
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return tomcatProperties;
    }

    /**
     * <b>loadConfigFromCommand</b>
     * <p>获取JVM命令的数据参数配置</p>
     * @return
     * @exception
     */
    private static TomcatProperties loadConfigFromCommand(TomcatProperties tomcatProperties) {
        try {
            for(String param:GlobalConfiguration.PROPERTIES_CONFIGURATION){
                String value = System.getProperty(GlobalConfiguration.DEFAULT_LOAD_CONFIG_TOMCAT_COMMAND+param,
                    GlobalConfiguration.DEFAULT_PORT.toString());
                Field field = TomcatProperties.class.getField(param);
                field.setAccessible(true);
                field.set(TomcatProperties.class.newInstance(),value);
            }
        } catch (NoSuchFieldException | InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void main(String[] args){
        TomcatBean tomcatBean = LoadConfigHandler.loadConfig(TomcatBootType.DEBUG);
        System.out.println(tomcatBean);
    }
}
